package router

import (
	"net/http"

	"gitee.com/flwwsg/apiserver/handler/sd"
	"gitee.com/flwwsg/apiserver/handler/user"
	"gitee.com/flwwsg/apiserver/router/middleware"
	"github.com/gin-gonic/gin"
)

//载入中间件
func Load(g *gin.Engine, mv ...gin.HandlerFunc) *gin.Engine {
	g.Use(gin.Recovery())
	g.Use(middleware.NoCache)
	g.Use(middleware.Options)
	g.Use(middleware.Secure)
	g.Use(mv...)
	//404
	g.NoRoute(func(context *gin.Context) {
		context.String(http.StatusNotFound, "invalid api route")
	})
	//health check
	svcd := g.Group("/sd")
	svcd.GET("/health", sd.HealthCHeck)
	svcd.GET("/disk", sd.DiskCheck)
	svcd.GET("/cpu", sd.CPUCheck)
	svcd.GET("/ram", sd.RAMCheck)

	//user handler
	u := g.Group("/v1/user")
	u.Use(middleware.AuthMiddleware())
	u.POST("", user.Create)
	u.DELETE("/:id", user.Delete)
	u.PUT("/:id", user.Update)
	u.GET("", user.List)
	u.GET("/:username", user.Get)

	//login
	g.POST("/login", user.Login)
	return g
}
